﻿using System.Collections.Generic;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvOpenDistricts.StorageIsolated;
using MyFloridaHouse.libSvUtilities;
using System;
using System.Windows;

namespace MyFloridaHouse.libSvOpenDistricts.Districts
{
    public class SnapshotFile
    {
        public int districtCount { get; set; }
        public string planName { get; set; }
        public string planType { get; set; }
        public DateTime snapshotTime { get; set; }
        public List<string> condensedList { get; set; }
    }

    public static class Snapshot
    {

        public static void Save(string planName, string planType, int districtCount, List<string> condensedList)
        {
            SnapshotFile snapshotFile = new SnapshotFile();
            snapshotFile.districtCount = districtCount;
            snapshotFile.planName = planName;
            snapshotFile.planType = planType;
            snapshotFile.snapshotTime = DateTime.Now;
            snapshotFile.condensedList = condensedList;
            byte[] serializedSnapshot = SerializationManager.Serialize(snapshotFile);
            byte[] serializedAndCompressedSnapshot = CompressionManager.Compress(serializedSnapshot);
            IsolatedStorageManager.SaveData(serializedAndCompressedSnapshot, Constants.IsolatedSaveSnapshotDirectoryName, Constants.IsolatedSnapshotFileName);
        }

        public static SnapshotFile Load()
        {
            if (!IsolatedStorageManager.FileExists(Constants.IsolatedSaveSnapshotDirectoryName, Constants.IsolatedSnapshotFileName))
            {
                throw new Exception("Snapshot load failed, file doesn't exist");
            }
            else
            {
                byte[] serializedAndCompressedSnapshot = IsolatedStorageManager.LoadBinaryData(Constants.IsolatedSaveSnapshotDirectoryName, Constants.IsolatedSnapshotFileName);
                byte[] serializedSnapshot = CompressionManager.DecompressToByteArray(serializedAndCompressedSnapshot);
                SnapshotFile snapshotFile = (SnapshotFile)SerializationManager.Deserialize(typeof(SnapshotFile), serializedSnapshot);

                if (null == snapshotFile)
                {
                    throw new Exception("Snapshot load failed, unable to read file from isolated storage");
                }

                if (null == snapshotFile.districtCount)
                {
                    throw new Exception("Snapshot load failed, no districts in file");
                }

                if (null == snapshotFile.condensedList)
                {
                    throw new Exception("Snapshot load failed, no districts with shapes in them");
                }

                if (null == snapshotFile.snapshotTime)
                {
                    throw new Exception("Snapshot load failed, no snapshot time recorded, this should never happen");
                }

                if (null == snapshotFile.planName)
                {
                    snapshotFile.planName = "undefined";
                }

                if (null == snapshotFile.planType)
                {
                    snapshotFile.planType = "undefined";
                }
                return snapshotFile;
            }
        }

    }
}
